package com.zhong.controller;

import com.zhong.po.Emp;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

//uri  /api/emp/add
//url  http://localhost:8080/api/emp/add

@Controller                        //Controller： 标识这个类就是一个控制层
@ResponseBody                      //以json格式返回数据   键值对
@RequestMapping("/api/emp")       //请求映射
public class EmpController {

    @RequestMapping("/add")  //method不写，默认任何请求方式均可
    public String add() {
        return "这是个新增方法";
    }

    //请求方式： GET POST DELETE PUT
    //method: 标注请求方式
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    public String delete() {
        return "这是个DELETE方法";
    }

    //@RequestParam: 被注释的字段，是必填； 只能使用在单个的字段上，不能使用在对象上
    //Required String parameter 'name' is not present"
    @RequestMapping(value = "/view", method = RequestMethod.GET)
    public String view(String name, String age) {
        return "name: " + name + "====age:  " + age;
    }

    @RequestMapping(value = "/view1", method = RequestMethod.GET)
    public Emp view(@RequestParam Emp emp) {
        return emp;
    }

    //@RequestBody 以json格式接收数据   键值对
    @RequestMapping(value = "/list", method = RequestMethod.POST)
    public Emp list(@RequestBody Emp emp) {
        emp.setId(10);
        System.err.println(emp);
        return emp;
    }

    //@PathVariable: 从地址栏获取参数
    @RequestMapping(value = "/{id}/update", method = RequestMethod.GET)
    public int update(@PathVariable int id) {
        System.err.println(id);
        return id;
    }

}
